package com.hongtech.modules.system.controller;

import com.hongtech.common.util.CommonUtil;
import com.hongtech.common.util.TokenUtil;
import com.hongtech.modules.common.BaseController;
import com.hongtech.modules.system.dto.SysUserDTO;
import com.hongtech.modules.system.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户中心控制器
 */
@Controller
@RequestMapping("/system/center")
public class CenterController extends BaseController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 个人信息
     */
    @RequestMapping("/info")
    public String info(Model model, HttpServletRequest request) {
        SysUserDTO sysUser = sysUserService.get(TokenUtil.getToken(request).getUid());
        model.addAttribute("sysUser", sysUser);
        return "system/center/info";
    }

    /**
     * 修改密码页面
     */
    @RequestMapping("/pwd")
    public String pwd() {
        return "system/center/pwd";
    }

    /**
     * 修改密码
     */
    @RequestMapping("/doChangePwd")
    public String doChangePwd(HttpServletRequest request, String password, String newPassword,
            String confirmPassword, RedirectAttributes redirectAttributes) {
        String redirectUrl = "/system/center/pwd";
        if (StringUtils.isBlank(password) || StringUtils.isBlank(newPassword)
                || StringUtils.isBlank(confirmPassword)) {
            redirectAttributes.addFlashAttribute("msg", "客户端提交数据不能为空.");
            return redirectTo(redirectUrl);
        }
        SysUserDTO user = sysUserService.get(TokenUtil.getToken(request).getUid());
        if (!user.getPassword().equals(CommonUtil.md5(password))) {
            redirectAttributes.addFlashAttribute("msg", "旧密码输入错误.");
            return redirectTo(redirectUrl);
        }
        if (!confirmPassword.equals(newPassword)) {
            redirectAttributes.addFlashAttribute("msg", "两次输入的密码不一致.");
            return redirectTo(redirectUrl);
        }
        user.setPassword(CommonUtil.md5(newPassword));
        sysUserService.update(user);
        redirectAttributes.addFlashAttribute("info", "密码修改成功.");
        return redirectTo(redirectUrl);
    }

    /**
     * 更新用户
     */
    @RequestMapping("/updateUser")
    public String updateUser(SysUserDTO sysUserDTO, Model model, RedirectAttributes redirectAttributes) {
        SysUserDTO user = sysUserService.get(sysUserDTO.getId());
        if (StringUtils.isNotBlank(user.getUserImg())) {
            user.setUserImg(sysUserDTO.getUserImg());
        }
        sysUserService.update(user);
        model.addAttribute("sysUser", user);
        redirectAttributes.addFlashAttribute("info", "更新成功.");
        return redirectTo("/system/center/info");
    }

}
